import contextlib
import os
import struct
import wave
import librosa
import numpy as np

# files 文件路径
# time 为语音总长度
def red_time(files, times):
    for path, dir_name, file_xm in os.walk(files):
        for name in file_xm:
            if name.endswith('wav'):
                path_wav = os.path.join(path, name)
                # with contextlib.closing(wave.open(path_wav, 'rb')) as f:
                #     frames = f.getnframes()
                #     rate = f.getframerate()
                #     duration = frames / float(rate)

                duration = librosa.get_duration(filename=path_wav)    
                # 判断当时语音长度
                
                if times > duration:
                    os.remove(path_wav)


if __name__ == "__main__":
    file_path = r'C:\Users\zz\Desktop\热词唤醒\keyword-spotting_2\data_CTC\words\zhongguo'
    file_time = 1
    red_time(file_path, file_time)
    print('完成')
